/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SelectSubtitle.java
 *
 * Created on 20 Μαϊ 2011, 9:03:05 μμ
 */
package tools.download.subtitles;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import myComponents.myGUI.MyDraggable;
import tools.download.subtitles.Subtitle;

/**
 *
 * @author lordovol
 */
public class SelectSubtitle extends MyDraggable {

  DefaultComboBoxModel comboModel = new DefaultComboBoxModel();
  private final ArrayList<Subtitle> subs;
  public Subtitle subtitle;

  /** Creates new form SelectSubtitle */
  public SelectSubtitle(ArrayList<Subtitle> subs) {
    Collections.sort(subs);
    this.subs = subs;
    comboModel = new DefaultComboBoxModel(subs.toArray());
    initComponents();
    cb_subs.setRenderer(new SubtitleListRenderer());
    setLocationRelativeTo(null);
    setVisible(true);
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    outer = new javax.swing.JPanel();
    inner = new javax.swing.JPanel();
    cb_subs = new javax.swing.JComboBox();
    jLabel1 = new javax.swing.JLabel();
    bt_cancel = new myComponents.myGUI.buttons.MyButtonCancel();
    bt_ok = new myComponents.myGUI.buttons.MyButtonOk();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    outer.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

    inner.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

    cb_subs.setModel(comboModel);

    javax.swing.GroupLayout innerLayout = new javax.swing.GroupLayout(inner);
    inner.setLayout(innerLayout);
    innerLayout.setHorizontalGroup(
      innerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(innerLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(cb_subs, 0, 383, Short.MAX_VALUE)
        .addContainerGap())
    );
    innerLayout.setVerticalGroup(
      innerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(innerLayout.createSequentialGroup()
        .addGap(22, 22, 22)
        .addComponent(cb_subs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(34, Short.MAX_VALUE))
    );

    jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD, jLabel1.getFont().getSize()+2));
    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setText("Select the subtitle to download");

    bt_cancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bt_cancelActionPerformed(evt);
      }
    });

    bt_ok.setToolTipText("Download");
    bt_ok.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bt_okActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout outerLayout = new javax.swing.GroupLayout(outer);
    outer.setLayout(outerLayout);
    outerLayout.setHorizontalGroup(
      outerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(outerLayout.createSequentialGroup()
        .addContainerGap()
        .addGroup(outerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(outerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(outerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(outerLayout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36))
              .addGroup(outerLayout.createSequentialGroup()
                .addComponent(inner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap()))
            .addComponent(bt_cancel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addComponent(bt_ok, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
    );
    outerLayout.setVerticalGroup(
      outerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(outerLayout.createSequentialGroup()
        .addGroup(outerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(outerLayout.createSequentialGroup()
            .addGap(9, 9, 9)
            .addComponent(jLabel1))
          .addComponent(bt_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGap(18, 18, 18)
        .addComponent(inner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
        .addComponent(bt_ok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(outer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(outer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void bt_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelActionPerformed
    dispose();
  }//GEN-LAST:event_bt_cancelActionPerformed

  private void bt_okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_okActionPerformed
    this.subtitle = (Subtitle) cb_subs.getSelectedItem();
     dispose();
  }//GEN-LAST:event_bt_okActionPerformed
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private myComponents.myGUI.buttons.MyButtonCancel bt_cancel;
  private myComponents.myGUI.buttons.MyButtonOk bt_ok;
  private javax.swing.JComboBox cb_subs;
  private javax.swing.JPanel inner;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JPanel outer;
  // End of variables declaration//GEN-END:variables

  private static class SubtitleListRenderer extends JLabel implements ListCellRenderer {

    public SubtitleListRenderer() {
      setOpaque(true);
      setBorder(BorderFactory.createEmptyBorder());
      setHorizontalAlignment(LEFT);
      setPreferredSize(new Dimension(this.getWidth(), 20));
    }

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
      if (isSelected) {
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        setFont(getFont().deriveFont(Font.BOLD));
      } else {
        setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
        setFont(getFont().deriveFont(Font.PLAIN));
      }
      if (value instanceof Subtitle) {
        Subtitle sub = (Subtitle) value;
        setText(sub.toString());
        setIcon(sub.getIcon());
        setBackground(sub.getBgColor());
      } else {
        setText(value.toString());
      }

      return this;
    }
  }
}
